package com.musicserver.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.musicserver.common.R;
import com.musicserver.mapper.SongMapper;
import com.musicserver.model.domain.SongList;
import com.musicserver.model.request.SongListRequest;
import com.musicserver.service.SongListService;
import com.musicserver.mapper.SongListMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
* @author tch001
* @description 针对表【song_list】的数据库操作Service实现
* @createDate 2025-09-18 08:16:14
*/
@Service
public class SongListServiceImpl extends ServiceImpl<SongListMapper, SongList>
    implements SongListService{

    @Autowired
    private SongListMapper songListMapper;

    @Autowired
    private SongMapper songMapper;


    @Override
    public R songList(String style) {
        QueryWrapper<SongList> queryWrapper = null;
        if(StringUtils.hasLength(style)){//判断歌单分类参数是否为空，如果为空则不用构造 QW 对象封装查询条件。
            queryWrapper = new QueryWrapper<>();
            queryWrapper.like("style", style);
        }
        return R.success(null, songListMapper.selectList(queryWrapper));
    }

    @Override
    public R addSongList(SongListRequest addSongListRequest) {
        SongList songList = new SongList();
        BeanUtils.copyProperties(addSongListRequest, songList);// DTO -> PO
        songList.setPic("/img/songListPic/default.jpg");
        if(songListMapper.insert(songList) > 0)
            return R.success("添加成功");
        else
            return R.error("添加失败");
    }

    @Override
    public R updateSongListMsg(SongListRequest updateSongListRequest) {
        SongList songList = new SongList();
        BeanUtils.copyProperties(updateSongListRequest, songList);// DTO -> PO
        if(songListMapper.updateById(songList) > 0)
            return R.success("更新成功");
        else
            return R.error("更新失败");
    }

    @Override
    public R deleteSongList(Integer id) {
        if (songListMapper.deleteById(id) > 0) {
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

    @Override
    public R deleteSongLists(String[] ids) {
        //把数组中参数组织成SQL语句需要的格式
        StringBuffer sb = new StringBuffer();
        for (String i:ids) {
            sb.append(i+",");
        }
        sb.deleteCharAt(sb.length()-1);
        if (songListMapper.deleteByIds(sb.toString()) > 0) {
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

    @Override
    public R likeTitle(String title) {
        QueryWrapper<SongList> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", title); // where title like '%title%'
        return R.success(null, songListMapper.selectList(queryWrapper));
    }

    @Override
    public R likeStyle(String style) {
        QueryWrapper<SongList> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("style", style);
        return R.success(null, songListMapper.selectList(queryWrapper));
    }

}




